<template>
	<z-paging ref="paging" v-model="indexList" @query="queryList" :show-loading-more-no-more-view="false" :refresher-enabled="false">
		<template slot="top">
			<cl-navbar autoBack title="" placeholder bgColor="#FF6052" :title="title" :isbgColor="false" leftIconColor="#ffffff" rightIcon='/static/icon/gengduo.svg'  @rightClick="goFeature">
			</cl-navbar>
		</template>
		<view class="redInfoView">
			<view class="infoview">
				<cl-avatar
					:src="user_info && user_info.avatar_url"
					size="120rpx"></cl-avatar>
				<text class="namered">{{user_info.nick_name}}共{{current?'收到':'发出'}}</text>
				<view class="moneyView">
					<text class="moneyValue">{{amount}}</text>
					<text class="moneyUnit">元</text>
				</view>
				<view class="count">{{current?'收到红包':'发出红包'}}总个数{{count}}个</view>
			</view>
			<view style="position: absolute;top: 30rpx;right: 30rpx;font-size: 26rpx;color: chocolate;display: flex;flex-direction: column;align-items: center;" @click="selctYear">
				<view>{{year}}年</view>
				<u-icon name="arrow-down" color="chocolate" size="26rpx"></u-icon>
			</view>
		</view>
		
		<view :key="index" v-for="(item, index) in indexList">
			<view class="listitem" v-if="current === 0">
				<view class="listitem__centent">
					<view class="listitem__centent__left">
						<cl-avatar
							:src="item.red_packet_info && item.red_packet_info.user && item.red_packet_info.user.avatar_url"
							size="100rpx"></cl-avatar>
					</view>
					<view class="listitem__centent__right">
						<text class="listitem__centent__right__nickname">{{item.red_packet_info && item.red_packet_info.user && item.red_packet_info.user.nick_name}}</text>
						<view class="listitem__centent__right__content">
							{{item.create_time|formatTime}}
						</view>
					</view>
				</view>
				<view class="listitem__controls">
					<text>{{item.amount}}元</text>
				</view>
			</view>
			<view class="listitem" v-if="current === 1">
				<view class="listitem__centent">
					<view class="listitem__centent__right">
						<text class="listitem__centent__right__nickname">{{item.packet_type == '0'?'普通红包':'拼手气红包'}}</text>
						<view class="listitem__centent__right__content">
							{{item.create_time|formatTime}}
						</view>
					</view>
				</view>
				<view class="listitem__controls">
					<text>{{item.amount}}元</text>
					<text style="font-size: 26rpx;color: #595A66;">{{item.quantity - item.remaining_count}}/{{item.quantity}}</text>
				</view>
			</view>
		</view>
		<view>
			<u-action-sheet :actions="selcetData" closeOnClickOverlay safeAreaInsetBottom @close="selectShow = false"
				lineColor="#ffffff" round="20rpx" :show="selectShow" :cancelText="$t('common.cancelText')"
				@select="selectShowClick"></u-action-sheet>
			<u-popup :show="yearShow" @close="yearShow = false" mode="center" bgColor="transparent">
				<view style="max-height: 80vh;overflow-y: auto;width: 650rpx;border-radius: 20rpx;background: #1E1F30;">
					<view style="margin: 40rpx 0;text-align: center;font-weight: 500;font-size: 28rpx;">选择年份</view>
					<view class="cellviewGrop" v-for="(item,index) in years" :key="index" @click="setYear(item)">
						<view class="cellview" >
							<view class="cellview__left">
								<text class="cellview__left__title">{{item}}</text>
							</view>
							<view class="cellview__right">
								<image src="/static/icon/select.png" mode="" style="width: 36rpx;height: 36rpx;" v-if="year == item"></image>
								<image src="/static/icon/no-select.png" mode="" style="width: 36rpx;height: 36rpx;" v-else></image>
							</view>
						</view>
					</view>
				</view>
			</u-popup>
		</view>
	</z-paging>
</template>

<script>
	import $T from '@/common/tools/time.js';
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				amount:'0.00',
				count:0,
				year:'2024',
				years:[],
				yearShow:false,
				title:"收到的红包",
				indexList: [],
				selcetData: [{
					name: '收到的红包',
					value: 0
				},{
					name: '发出的红包',
					value: 1
				}],
				current:0,//当前选中
				selectShow: false,
			};
		},
		filters: {
			formatTime(value) {
				if (!value) return ''
				return $T.gettime(value);
			},
		},
		computed: {
			...mapState({
				user_info: state => state.user.user_info,
			}),
		},
		onLoad() {
			// 获取当前年份
			const currentYear = new Date().getFullYear();
			this.year = currentYear
			// 往前取十年
			const yearsArray = [];
			for (let i = 0; i < 10; i++) {
			    yearsArray.push(currentYear - i);
			}
			console.log(yearsArray);
			this.years = yearsArray
		},
		methods: {
			selctYear(){
				this.yearShow = true
			},
			setYear(item){
				this.year = item
				this.yearShow = false
				this.$refs.paging.reload()
			},
			selectShowClick(e) {
				this.title = e.name
				this.current = e.value
				this.$refs.paging.reload()
			},
			goFeature(){
				this.selectShow = true
			},
			async queryList(pageNo, pageSize) {
				if(this.current === 0){
					//收到的
					uni.$api.user.getReceiveRecordList({
						page:pageNo,
						limit:pageSize,
						year:this.year
					}).then(res=>{
						if(pageNo == 1){
							this.amount = "0.00"
							this.count = 0
						}
						if(res && res.code == 200){
							this.amount = res.data.total_amount || "0.00"
							this.count = res.data.receive_record_count
							this.$refs.paging.complete(res.data.receive_record_list)
						}else if(res && res.code == 2003){
							this.$refs.paging.complete([])
						}else{
							this.$refs.paging.complete(false)
						}
					}).catch(e=>{
						if(pageNo == 1){
							this.amount = "0.00"
							this.count = 0
						}
						this.$refs.paging.complete(false)
					})
				}else if(this.current === 1){
					//发出的
					uni.$api.user.getSendRecordList({
						page:pageNo,
						limit:pageSize,
						year:this.year
					}).then(res=>{
						if(pageNo == 1){
							this.amount = "0.00"
							this.count = 0
						}
						if(res && res.code == 200){
							this.amount = res.data.total_amount || "0.00"
							this.count = res.data.send_record_count
							this.$refs.paging.complete(res.data.send_record_list)
						}else if(res && res.code == 2003){
							this.$refs.paging.complete([])
						}else{
							this.$refs.paging.complete(false)
						}
					}).catch(e=>{
						if(pageNo == 1){
							this.amount = "0.00"
							this.count = 0
						}
						this.$refs.paging.complete(false)
					})
				}
			},
		},
	}
</script>

<style lang="scss">
	.cellviewGrop {
		.cellview {
			display: flex;
			justify-content: space-between;
			padding: 0 30rpx;
			height: 110rpx;
	
			&__left {
				display: flex;
				align-items: center;
	
	
				&__title {
					font-size: 28rpx;
					color: #FFFFFF;
				}
			}
	
			&__right {
				display: flex;
				align-items: center;
	
				&__content {
					font-size: 28rpx;
					color: #61626e;
				}
			}
		}
	}
	.redInfoView {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 100rpx 30rpx 0;

		.infoview {
			display: flex;
			flex-direction: column;
			align-items: center;

			.namered {
				font-size: 32rpx;
				font-weight: bold;
				margin-top: 10rpx;
			}
			.moneyView {
				display: flex;
				align-items: flex-end;
				margin-top: 20rpx;
				.moneyValue {
				    font-size: 80rpx;
				    color: #FC8F2C;
				    font-weight: 700;
				    line-height: 1; /* 避免文字顶部多余的间距 */
				}
				
				.moneyUnit {
				    font-size: 28rpx;
				    color: #FC8F2C;
				    margin-left: 10rpx; /* 添加间距来美化布局 */
				}
			}
			.count{
				margin: 50rpx 0 30rpx;
				color: #595A66;
				font-size: 30rpx;
				text-align: center;
			}
		}
	}

	.listitem {
		display: flex;
		justify-content: space-between;
		padding: 20rpx 30rpx;
		position: relative;
		
		&__centent {
			display: flex;
			align-items: center;
			&__left {
				margin-right: 20rpx;
			}
			&__right {
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: center;
				&__nickname {
					font-size: 30rpx;
				}

				&__content {
					margin-top: 5rpx;
					font-size: 26rpx;
					color: #595A66;
				}
			}
		}

		&__controls {
			display: flex;
			flex-direction: column;
			font-size: 32rpx;
			font-weight: 500;
			margin-top: 15rpx;
			align-items: flex-end;
		}

	}
</style>